package com.sky.demo.aes;

import java.math.BigInteger;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import com.alibaba.excel.util.StringUtils;
import sun.misc.BASE64Decoder;

public class AESUtil {
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    public AESUtil() {
    }

    public static String binary(byte[] bytes, int radix) {
        return (new BigInteger(1, bytes)).toString(radix);
    }

    public static String base64Encode(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static byte[] base64Decode(String base64Code) throws Exception {
        return base64Code != null && base64Code.length() > 0 ? (new BASE64Decoder()).decodeBuffer(base64Code) : null;
    }

    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(1, new SecretKeySpec(encryptKey.getBytes(), "AES"));
        return cipher.doFinal(content.getBytes("utf-8"));
    }

    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(2, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
       return encryptStr != null && encryptStr.length() > 0 ? aesDecryptByBytes(base64Decode(encryptStr), decryptKey) : null;

    }

    public static void main(String[] args) throws Exception {


        StringBuilder str = new StringBuilder("PeI2uW3oYiWRgG+yGSstLg==\n" +
                "zkdvCWUu4FNt4lqwIH23Zw==\n" +
                "zkdvCWUu4FNt4lqwIH23Zw==\n" +
                "+fsfxXQ0Uq61s3fERT4Lxw==\n" +
                "CwIbBM+bIVRtwHgiUk9dWA==\n" +
                "a+CPSyv0r6QxgNAE8t6S8g==\n" +
                "CCQt6OzUeXbZyaSQpEbdYA==\n" +
                "CCQt6OzUeXbZyaSQpEbdYA==\n" +
                "ZNIHo3aVUVazLokNrYXbNQ==\n" +
                "/mJ4lW/iCoZXOrbhlY6Nfg==\n" +
                "mKKuD5j2kGQ96y7Zs85c9A==\n" +
                "HNPLODM/X3V4Y8vjrulq0A==\n" +
                "iarMCKK7F/qlahVJ4YAzmw==\n" +
                "DXTZNwAAFh3kE5wJLF9DdVjQuecsyEYuGepCmwIvbgY=\n" +
                "D6Th0KpRrAD96gyRYGqVHg==\n" +
                "2523xDFgQN+kE0fdd26TOg==\n" +
                "CEMJqf+Sw8GZfyrdtUVR1Q==\n" +
                "6KZLpMpqAnd8GV8Nc+9/Tg==\n" +
                "6KZLpMpqAnd8GV8Nc+9/Tg==\n" +
                "d00YNGuW7VfPgf+RZG9UlQ==\n" +
                "iGUfnzg6vwtwQhDi5Ye+yw==\n" +
                "QAr2UOc0oVc0JLcAQLgHGQ==\n" +
                "tN6T45btYAw21DOHepm9xw==\n" +
                "y8vw7ZQmowaJgtcJLI2qhQ==\n" +
                "zkdvCWUu4FNt4lqwIH23Zw==\n" +
                "zkdvCWUu4FNt4lqwIH23Zw==\n" +
                "n822W6pl3TO/KaLuh6wrgw==\n" +
                "2k37urphkU/44I/0B8h8oQ==\n" +
                "zask+AF8xEQ+IAgm7ZLnGg==\n" +
                "0dYLIJI8M8skDVCVOAfutA==\n" +
                "mKKuD5j2kGQ96y7Zs85c9A==\n" +
                "zkdvCWUu4FNt4lqwIH23Zw==\n" +
                "zkdvCWUu4FNt4lqwIH23Zw==\n" +
                "2k37urphkU/44I/0B8h8oQ==\n" +
                "CDLZxNOB200qSegPtLI12Q==\n" +
                "Qhx7m7t+4vvuD4aK4hOe7A==\n" +
                "FVZAllwm+ep7aU+lQOL0QQ==\n" +
                "v53RHQ+kGp1bk4ltaWQxCw==\n" +
                "acfbaJSuOsI5klN2D+WAMg==\n" +
                "ilOAVn9qU5WHVzt7jmCdPA==\n" +
                "l2LV0cydKeLdG0Ym/i1NtI8qxc9PiKpC7GfXRdzQ784=\n" +
                "aFqxugSwcdfyYJdFCm46Bw==\n" +
                "d00YNGuW7VfPgf+RZG9UlQ==\n" +
                "jyyNri1NazA8Bw2uDvf8WQ==\n" +
                "VkMotg6piLDQJs0rUq6IRw==\n" +
                "QAr2UOc0oVc0JLcAQLgHGQ==\n" +
                "juZc3u2e4i2Xk3UJOP9cqo8qxc9PiKpC7GfXRdzQ784=\n" +
                "1NPjuDNRBvYUxz254OSeOg==\n" +
                "1NPjuDNRBvYUxz254OSeOg==\n" +
                "8bGxYrnWQB0IetN/kMjLNQ==\n" +
                "Ud2WwCXvL0RsElnfqmmqiQ==\n" +
                "8N8Gvsg2NrJW5+gH3n1Kxw==\n" +
                "Y/ICaWsMWGYIyH6XfLE2/1jQuecsyEYuGepCmwIvbgY=\n" +
                "x+kE7RdQD/ePPvcfqP8XoA==\n" +
                "USJ33UqaFxWduUdFyh2VSg==\n" +
                "sXPoyWaObkhknCrOwmZA0w==\n" +
                "8N8Gvsg2NrJW5+gH3n1Kxw==\n" +
                "grhKCDTmXKrkTYdPdzVqVQ==\n" +
                "Ud2WwCXvL0RsElnfqmmqiQ==\n" +
                "OwhaAvaBN+NMM3WaO0CLFw==\n" +
                "Ud2WwCXvL0RsElnfqmmqiQ==\n" +
                "WX54RfYqORvPxEmzp6kUqg==\n" +
                "DoIVtmR2e8+jXzpYgWKV8w==\n" +
                "O1Qwm2zVmOANHowgE2MOZQ==\n" +
                "DMU340Iph9xsO9dUgE4hhQ==\n" +
                "d00YNGuW7VfPgf+RZG9UlQ==\n" +
                "CDLZxNOB200qSegPtLI12Q==\n" +
                "li7t4CDMXZR4uVmE3EVwuQ==\n" +
                "zkdvCWUu4FNt4lqwIH23Zw==\n" +
                "zkdvCWUu4FNt4lqwIH23Zw==\n" +
                "5LFyEOoBjNMKKAjTMCOiNA==\n" +
                "OwlfYzSkjjlDGMIWVsupaQ==\n" +
                "8FKkXg4s8JVlE5O/TIhZow==\n" +
                "D6Th0KpRrAD96gyRYGqVHg==\n" +
                "D6Th0KpRrAD96gyRYGqVHg==\n" +
                "d3/a4l+cBW928tKl1auuCw==\n" +
                "ZNIHo3aVUVazLokNrYXbNQ==\n" +
                "ZNIHo3aVUVazLokNrYXbNQ==\n" +
                "e4hdRs0nVZPqfBHo1aVz+w==\n" +
                "e4hdRs0nVZPqfBHo1aVz+w==\n" +
                "e4hdRs0nVZPqfBHo1aVz+w==\n" +
                "laKcu9aft4RARVgEYXUEHA==\n" +
                "ClsvgULubChOloisypKQqw==\n" +
                "xePNhd/Llr8MS9SohZunLA==\n" +
                "TcJoNiZQuOXO/nUwxYCVCQ==\n" +
                "Yor/v1HwWwSbquLBRe4mrQ==\n" +
                "HNPLODM/X3V4Y8vjrulq0A==\n" +
                "HNPLODM/X3V4Y8vjrulq0A==\n");

        String[] strs = str.toString().split("\n");
        for(String s : strs){
            if(StringUtils.isEmpty(s)){
                System.out.print("\n");
            }else{
                String var2 = AESUtil.aesDecrypt(s, "h2wGC4pNGzWYgiEW");
                System.out.println(var2);
            }
        }
    }
}
